package com.example.banksys;

import DataBase.DBMsg;
import DataBase.myDB;
import handler.queryHandler;
import handler.tellerbusinessHandler;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class BusinessPageController {
    @FXML
    public ComboBox<String> typeChoice;
    @FXML
    public AnchorPane mainPane;
    
    public Button launch;
    public PasswordField passwd;
    public TextField account;
    public Label descriptionlabel;
    public TextField money;
    public ComboBox<String> nameChoice;

    static class typeset{
        String name;
        String description;

        long type_id;

        public typeset(String name,String description,long type_id){
            this.description=description;
            this.name=name;
            this.type_id=type_id;
        }
    }

    public ArrayList<typeset> descriptions;

    public void initialize(){
        typeChoice.getItems().addAll("save","take","loan","financialProduct");

        typeChoice.setOnAction(new EventHandler() {
            @Override
            public void handle(Event event) {
                try {
                    descriptionlabel.setText("");
                    myDB mydb=new myDB("banksystem");
                    Statement t=mydb.getStatement();
                    String sql="select * from trade_type where type_category = '"+typeChoice.getValue()+"'";
                    ResultSet res=t.executeQuery(sql);
                    descriptions=new ArrayList<>();
                    int j=nameChoice.getItems().size();
                    //这里好像removeall()方法有问题，只能一个一个移除了，和UI绑定更新有关系应该是
                    if (j > 0) {
                        nameChoice.getItems().subList(0, j).clear();
                    }
                   descriptions.clear();
                    while(res.next()){
                        nameChoice.getItems().add(res.getString("type_name"));
                        descriptions.add(new typeset(res.getString("type_name"), res.getString("describe"), res.getLong("type_ID")));
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        nameChoice.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                String ordertype=nameChoice.getValue();
                for (typeset item:descriptions) {
                    if(item.name.equals(ordertype)){
                        descriptionlabel.setText(item.description);
                        launch.setDisable(false);
                        return;
                    }
                }
            }
        });
        launch.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                queryHandler queryhandler=new queryHandler();
                Account currentAccount=new Account();
                if(account.getText().isEmpty()||money.getText().isEmpty()){
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.titleProperty().set("信息");
                    alert.headerTextProperty().set("账户ID和交易规模不能为空！");
                    alert.showAndWait();
                    return;
                }
                    double amount;
                try{
                    long accountID=Long.parseLong(account.getText());
                    amount=Double.parseDouble(money.getText());
                    currentAccount.setAccount_ID(accountID);
                    currentAccount.setPassword(passwd.getText());
                }catch (NumberFormatException e){
            /*
             这里处理一下没办法转换的情况不然要报错
             */
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.titleProperty().set("信息");
                    alert.headerTextProperty().set("请输入正确的账户ID和金额！");
                    alert.showAndWait();
                    return;
                }
                DBMsg ret= null;
                try {
                    ret = queryhandler.accountQuery(currentAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                if(ret.getState()==DBMsg.State.ERROR){
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.titleProperty().set("信息");
                    alert.headerTextProperty().set(ret.getInfo());
                    alert.showAndWait();

                }else{
                    tellerbusinessHandler tradehandler=new tellerbusinessHandler();
                    long typeid=0;
                    for (typeset item:descriptions) {
                        if(item.name.equals(nameChoice.getValue())){
                            typeid=item.type_id;
                        }
                    }
                    try {
                        DBMsg res= tradehandler.TradeHandle(currentAccount,amount,typeid,MainApplication.employeeID,typeChoice.getValue());
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.titleProperty().set("信息");
                        alert.headerTextProperty().set(res.getInfo());
                        alert.showAndWait();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });



    }




}
